/*
 * Copyright (C) 2019 Baidu, Inc. All Rights Reserved.
 */
package com.bootdo.hjh.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.hjh.domain.ProductType;
import com.bootdo.hjh.domain.TihuoOrderDO;
import com.bootdo.hjh.service.TihuoOrderService;

/**
 * 提货申请单
 *
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2019-04-13 16:13:13
 */

@Controller
@RequestMapping("/hjh/tihuoOrder")
public class TihuoOrderController {
	@Autowired
	private TihuoOrderService tihuoOrderService;

	@GetMapping()
	@RequiresPermissions("hjh:tihuoOrder:tihuoOrder")
	String TihuoOrder(){
	    return "hjh/tihuoOrder/tihuoOrder";
	}

	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("hjh:tihuoOrder:tihuoOrder")
	public PageUtils list(
			@RequestParam(value = "type", required = false) Integer type,
			@RequestParam(value = "tihuoNo", required = false) String tihuoNo,
			@RequestParam("offset") Integer offset,
			@RequestParam("limit") Integer limit){
		//查询列表数据
		HashMap<String, Object> params = new HashMap<>();
		params.put("offset", offset);
		params.put("limit", limit);
		if (type != null) {
			ProductType productType = ProductType.getByValue(type);
			if (!productType.equals(ProductType.UNKNOWN)) {
				params.put("productId", productType.getProductId());
			}
		}
		if (StringUtils.isNotBlank(tihuoNo)) {
			params.put("tihuoNo", tihuoNo);
		}
		Query query = new Query(params);
		List<TihuoOrderDO> tihuoOrderList = tihuoOrderService.list(query);
		int total = tihuoOrderService.count(query);
		PageUtils pageUtils = new PageUtils(tihuoOrderList, total);
		return pageUtils;
	}

	@GetMapping("/edit/{id}")
	@ResponseBody
	public R<TihuoOrderDO> edit(@PathVariable("id") Long id){
		TihuoOrderDO tihuoOrder = tihuoOrderService.get(id);
	    return new R(tihuoOrder);
	}

	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public R update(@RequestBody JSONObject fahuo){
		Integer id = fahuo.getInteger("id");
		Integer shippingStatus = fahuo.getInteger("shippingStatus");
		Integer shippingChannel = fahuo.getInteger("shippingChannel");
		String shippingSn = fahuo.getString("shippingSn");
		tihuoOrderService.updateFahuo(id, shippingStatus, shippingChannel, shippingSn);
		return R.ok();
	}
}
